/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import java.util.Iterator;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.TableColumn;

import com.google.code.cubeirc.common.Utils;
import com.google.code.cubeirc.config.Configuration;
import com.google.code.cubeirc.config.ConnectionSettings;
import com.google.code.cubeirc.connection.Connection;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;

public class QuickConnectForm extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table tbServers;
	private Button btnConnect;
	private Button btnClose;
	private TableColumn tblclmnName;
	private TableColumn tblclmnAddress;
	private TableColumn tblclmnPort;


	@Setter @Getter
	private Connection connection;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public QuickConnectForm(Shell parent, int style, Connection conn) {
		super(parent, style);
		setText("Quick connect to server");
		setConnection(conn);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		jbInit();
		this.shell.open();
		this.shell.layout();
		Display display = getParent().getDisplay();
		while (!this.shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return this.result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void jbInit() {
		this.shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		this.shell.setSize(352, 298);
		this.shell.setText(getText());
		this.shell.setLayout(new FormLayout());

		this.tbServers = new Table(this.shell, SWT.BORDER | SWT.FULL_SELECTION);
		this.tbServers.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				connect();
			}
		});
		FormData fd_tbServers = new FormData();
		fd_tbServers.top = new FormAttachment(0);
		fd_tbServers.left = new FormAttachment(0);
		fd_tbServers.bottom = new FormAttachment(0, 234);
		this.tbServers.setLayoutData(fd_tbServers);
		this.tbServers.setHeaderVisible(true);
		this.tbServers.setLinesVisible(true);

		this.btnConnect = new Button(this.shell, SWT.NONE);
		this.btnConnect.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				connect();
			}
		});
		this.btnConnect.setImage(SWTResourceManager.getImage(QuickConnectForm.class, "/com/google/code/cubeirc/resources/img_quickconnect.png"));
		FormData fd_btnConnect = new FormData();
		fd_btnConnect.top = new FormAttachment(this.tbServers, 6);
		fd_btnConnect.left = new FormAttachment(0);
		this.btnConnect.setLayoutData(fd_btnConnect);
		this.btnConnect.setText("Connect");

		this.btnClose = new Button(this.shell, SWT.NONE);
		this.btnClose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.dispose();
			}
		});
		fd_tbServers.right = new FormAttachment(this.btnClose, 0, SWT.RIGHT);
		this.btnClose.setImage(SWTResourceManager.getImage(QuickConnectForm.class, "/com/google/code/cubeirc/resources/img_exit.png"));
		FormData fd_btnClose = new FormData();
		fd_btnClose.top = new FormAttachment(this.tbServers, 6);

		this.tblclmnName = new TableColumn(this.tbServers, SWT.NONE);
		this.tblclmnName.setWidth(149);
		this.tblclmnName.setText("Name");

		this.tblclmnAddress = new TableColumn(this.tbServers, SWT.NONE);
		this.tblclmnAddress.setWidth(134);
		this.tblclmnAddress.setText("Address");

		this.tblclmnPort = new TableColumn(this.tbServers, SWT.NONE);
		this.tblclmnPort.setWidth(59);
		this.tblclmnPort.setText("Port");
		fd_btnClose.left = new FormAttachment(100, -81);
		fd_btnClose.right = new FormAttachment(100);
		this.btnClose.setLayoutData(fd_btnClose);
		this.btnClose.setText("Close");
		Display display = getParent().getDisplay();
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();

		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;

		shell.setLocation(x, y);

		initTable();

	}

	private void connect()
	{
		if (tbServers.getSelectionIndex() >= 0)
		{
			getConnection().Connect(Configuration.getCurrentUSConfig(), (ConnectionSettings)tbServers.getItem(tbServers.getSelectionIndex()).getData()  );
			shell.dispose();
		}
	}
	private void initTable()
	{
		Utils.asyncExec(getParent().getDisplay(), new Runnable() {

			@Override
			public void run() {
				for (Iterator<ConnectionSettings> it = Configuration.getCurrentCSSettings().iterator(); it.hasNext();)
				{
					ConnectionSettings cs = it.next();
					TableItem tb = new TableItem(tbServers,SWT.NORMAL);
					tb.setImage(SWTResourceManager.getImage(MainForm.class, "/com/google/code/cubeirc/resources/img_server.png"));
					tb.setText(0, cs.getConnectionName());
					tb.setText(1, cs.getServer());
					tb.setText(2, cs.getPort());
					tb.setData(cs);				    
				}
			}
		});

	}
}
